
<style>
.row select, .row textarea, .row input[type=text]{
	width: 400px;
}
</style>
<script>
var userid = <?php echo UserUtils::getUserId(); ?> ;
$(document).ready(function(){
function loadcbb(){
var dataId = <?php echo $id; ?>;
        $.ajax({
	                url: 'Dichvu/getJson',
	                type: 'POST',
	                dataType: 'JSON',
	                data: {id: dataId, iduser: userid},
	                success: function(result){
	                $('#price').html(result.data.gia);
	                $('#timedone').html(result.data.thoigian); 
	                if(result.data.ghichu == ""){
	               	 $('#luuy').html(result.data.ghichu);
	                }else{
	               	 $('#luuy').html("Lưu ý: " + result.data.ghichu);
	                }  
                }
        });
}
loadcbb();
    $('#id').change( function(){
        var dataId = $(this).val();
        $.ajax({
                url: 'Dichvu/getJson',
                type: 'POST',
                dataType: 'JSON',
                data: {id: dataId, iduser: userid},
                success: function(result){
                $('#price').html(result.data.gia);
                $('#timedone').html(result.data.thoigian);      
                if(result.data.ghichu == ""){
	               	 $('#luuy').html(result.data.ghichu);
	                }else{
	               	 $('#luuy').html("Lưu ý: " + result.data.ghichu);
	                }           
                }
        });
	});
	//$('#submit').attr('disabled',true);
	$('#alert:visible').hide();
	$('#alert-credit:visible').hide();
	function textchange() {
	var regex = /\D/;		
        var input = $("#imei").val();
        var array= input.split('\n');
        var text;
        var isError=0;
        var numRight=0;
        for(var i=0;i<array.length;i++)
        {     
        	if(array[i].length==0){}else   	
        	if(array[i].length != 15 || parseInt(array[i]).toString().length <0 || array[i].match(regex)!=null || isNaN(parseInt(array[i])) )
        	{
        		//alert("loi");
        		isError=1;	
        	}
        	else if(array[i].length==15){
        		//alert("dung");
        		numRight++;
        	}
        }
        // numRight la so Imei dung, tong tien = numRight * gia Credit, roi hien thi ra man hinh
        //alert(numRight + " x so Credit / Emei ");
        $('#total-imei').html(numRight);
        var price = $('#price').html();
        var total =(numRight * price).toFixed(4)/1;
        var credit=$('#current-credit').html();
        var checkcredit=credit-total;
        // neu trong list co loi, disable button add de user sua loi roi moi add
        if(isError==1)
        {
        	$('#alert').show();
        }else
        {
        	$('#alert:visible').hide();
        }
        if(checkcredit<0)
        {        	
        	$('#alert-credit').show();
        }else
        {
        	$('#alert-credit:visible').hide();
        }
        if(isError==0 && checkcredit>=0)
        {
        	$('#submit').attr('disabled',false);
        }else
        {
        	//$('#submit').attr('disabled',true);
        }
        $('#total-credit').html(total);
    }       
    // EDIT BELOW -- this will update while typing
    $("#imei").keyup(textchange);
    $("#dichvuform").submit(function() {
    	$("#imei").val($.trim($('#imei').val()));
    });
});
</script>
<?php
/* @var $this SiteController */
$this->pageTitle=Yii::app()->name. ' - Đặt hàng';
$this->breadcrumbs=array(
	'Dịch vụ'=>array('nhomdichvu/dichvu'),
	'Đặt hàng',
);
?>

<?php $this->widget('bootstrap.widgets.TbAlert', array(
        'block'=>true, // display a larger alert block?
        'fade'=>true, // use transitions?
        'closeText'=>'&times;', // close link text - if set to false, no close link is displayed
        'alerts'=>array( // configurations per alert type
            'success'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger
			'error'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'),
        ),
)); ?>
    
<div  style="width:36%;float:left;">
<div class="left-box-item">
<div class='title'>Thông tin</div>
<div style="margin:20px; font-size:12pt;">
	<div id="alert" style="color:red;font-weight:bold;">Kiểm tra lại list Imei</div>
	<div id="alert-credit" style="color:red;font-weight:bold;">không đủ Credit để thực hiện <br/><?php echo CHtml::link("NẠP CREDIT",array('/credit/napthecao'));?></div>
	
	<span style="width:120px; display:inline-block;">Credit hiện tại:</span><b><div id="current-credit" style="display:inline;"><?php echo $cre->sodu; ?></div> Credit </b><br/>
	<span style="width:120px; display:inline-block;">Số Imei : </span><b><div id="total-imei"  style="display:inline;">0</div> Imei</b><br/>	<span style="width:120px; display:inline-block;">Credit cần: </span><b><div id="total-credit"  style="display:inline;">0</div> Credit</b><br/></div></div></div><div class="" style="width:61%;float:right"><div class="right-box-item"><?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(	'id'=>'dichvuform',	'enableAjaxValidation'=>false,)); ?><h1>Đặt hàng</h1>
<div class="form"><div style="margin:20px"><div class='row'><span> <b>Dịch vụ</b> <b>:</b> <div class="row">	<?php 	//echo $form->hiddenField($models,'id',array()); ?><?php 

$this->widget(
    'bootstrap.widgets.TbSelect2',
    array(
        'name' => 'id',
		'data'=>$nhomdichvu,
		'val'=>$id,
        'options' => array(
            'placeholder' => 'Vui lòng nhập giá trị',
        ),
        'htmlOptions'=>array('style'=>'width:480px;', 'size'=>'15'),
    )
);
 ?>	</div> </span></div><div class='row'>	<div class='m-description'  style='line-height: 80px;'>		<span class='m-left'><label id="price" style='float:left;line-height: 80px;'><?php echo $gia; ?></label>
		<label  style='float:left;line-height: 80px;'> Credit</label>
		</span>		<div class='m-right'>		<span>Thời gian</span></br>		<span id="timedone" style="display:inline;"> </span>		</div>	</div></div></br><div class='row' id='ghichu'>		<span style='color:red; font-size: 16px;'>			<span id='luuy'></span>		</span></div></br>
<div class="row">	<span><b>IMEI: </b></span></br>		<textarea name='imei' id='imei'></textarea></div> <div class="row">
	<span><b>Ghi chú: </b></span></br>
	<input type='text' name='comment' id='comment'/>
</div>
<br/>
	<div class="row buttons">
	<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'inverse', 'label'=>'Đặt hàng','htmlOptions'=>array('id'=>'submit'))); ?>
		<?php // echo CHtml::submitButton( 'Order',array('id'=>'submit')); ?>
	</div>
<?php $this->endWidget(); ?>
</div></div>
</div>
</div>

